package com.ruoyi.my.service.impl;

import com.ruoyi.my.service.MyBucketService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MyBucketServiceImpl implements MyBucketService {

    @Resource
    private MinioClient minioClient;

    @Override
    public String add(String bucketName) {
        try {
            if (isBucketExits(bucketName)){
                return "文件目录已存在";
            }else {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                //todo:文件信息写入数据库
                return "创建成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Override
    public String remove(String bucketName) {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            //todo:删除数据库中相关信息
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    private boolean isBucketExits(String bucketName){
        try {
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
